//
//  ViewController.swift
//  QBLaunchAnimation
//
//  Created by 乐启榜 on 16/8/26.
//  Copyright © 2016年 乐启榜. All rights reserved.
//

// 注： 本Demo参考作者HenryCheng写的HWLoadingAnimation；
//      作者GitHub链接： https://github.com/Loveway/HWLoadingAnimation.git
//      谢谢HenryCheng

// Demo涉及的动画：
// 1、果冻动画； 2、旋转动画； 3、波浪动画； 4、放大缩小动画； 5、线条
// 画圆、画三角形、画矩形、画多边形

import UIKit

class ViewController: UIViewController,QBAnimationViewDelegate {

    var animationView = QBAnimationView(frame: CGRectZero)
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        addAnimationView()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func addAnimationView() {
        let size: CGFloat = 100.0
        animationView.frame = CGRect(x: CGRectGetWidth(self.view.frame)/2 - size/2, y: CGRectGetHeight(self.view.frame)/2 - size/2, width: size, height: size)
        animationView.parentFrmae = view.frame
        animationView.delegate = self
        self.view.addSubview(animationView)
    }
    
    func completeAnimation() {
        // 1、
        animationView.removeFromSuperview()
        view.backgroundColor = UIColor.colorWithHexString("#40e0b0")

        // 2、
        let label: UILabel = UILabel(frame: view.frame)
        label.text = "Welcome"
        label.textColor = UIColor.whiteColor()
        label.font = UIFont(name: "HelveticaNeue-Thin", size: 50.0)
        label.textAlignment = .Center
        label.transform = CGAffineTransformScale(label.transform, 0.25, 0.25)
        view.addSubview(label)
        
        //3、 
        weak var weakSelf = self
        guard let _weakSelf = weakSelf else{return}
        UIView.animateWithDuration(0.4, delay: 0.0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.1, options: UIViewAnimationOptions.CurveEaseInOut, animations: { 
            label.transform = CGAffineTransformScale(label.transform, 4.0, 4.0)
            }) { (finished) in
            _weakSelf.addTouchButton()
        }
    }
    
    
    func addTouchButton() {
        let button = UIButton()
        button.frame = CGRectMake(0.0, 0.0, view.bounds.width, view.bounds.height)
        button.addTarget(self, action: #selector(btnClick), forControlEvents: .TouchUpInside)
        view.addSubview(button)
    }
    
    func btnClick() {
        view.backgroundColor = UIColor.whiteColor()
        view.subviews.forEach { (
            $0.removeFromSuperview()
            )}
        animationView = QBAnimationView(frame: CGRectZero)
        addAnimationView()
    }
}

